package com.zy.openchat.core.model.group;

import android.os.Parcel;
import android.os.Parcelable;

import com.zy.openchat.core.manager.TelegramManager;

public class GroupMemberInfo implements Parcelable {
    //User identifier of the chat member.
    public long user_id;
    //Identifier of a user that invited/promoted/banned this member in the chat; 0 if unknown.
    public long inviter_user_id;
    //Point in time (Unix timestamp) when the user joined the chat.
    public long joined_chat_date;
    //Status of the member in the chat.
    public Group_ChatMemberStatus status;

    public String nickname;

    public static GroupMemberInfo optAdd() {
        GroupMemberInfo info = new GroupMemberInfo();
        info.user_id = -1;
        return info;
    }

    public static GroupMemberInfo optDel() {
        GroupMemberInfo info = new GroupMemberInfo();
        info.user_id = -2;
        return info;
    }

    public boolean isOptAdd() {
        return user_id == -1;
    }

    public boolean isOptDel() {
        return user_id == -2;
    }

    public boolean isCreatorRole(){
        if (status != null) {
            return status.getMemberState() == TelegramManager.GroupMemberState.GroupMemberState_Creator&&status.is_member;
        }
        return false;
    }


    public boolean isManagerRole() {
        if (status != null) {
            switch (status.getMemberState()) {
                case GroupMemberState_Administrator:
                    //管理员
                    return true;
                case GroupMemberState_Creator:
                    //创建者
                    if (status.is_member) {//创建者已不在群组
                        return true;
                    }
                    break;
                case GroupMemberState_Left:
                    //不在群组
                    break;
                case GroupMemberState_Member:
                    //普通成员
                    break;
                case GroupMemberState_Banned:
                    //被禁用
                    break;
                case GroupMemberState_Restricted:
                    //被禁言
                    break;
                default:
                    break;
            }
        }
        return false;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeLong(this.user_id);
        dest.writeLong(this.inviter_user_id);
        dest.writeLong(this.joined_chat_date);
        dest.writeParcelable(this.status, flags);
        dest.writeString(this.nickname);
    }

    public GroupMemberInfo() {
    }

    protected GroupMemberInfo(Parcel in) {
        this.user_id = in.readLong();
        this.inviter_user_id = in.readLong();
        this.joined_chat_date = in.readLong();
        this.status = in.readParcelable(Group_ChatMemberStatus.class.getClassLoader());
        this.nickname = in.readString();
    }

    public static final Creator<GroupMemberInfo> CREATOR = new Creator<GroupMemberInfo>() {
        @Override
        public GroupMemberInfo createFromParcel(Parcel source) {
            return new GroupMemberInfo(source);
        }

        @Override
        public GroupMemberInfo[] newArray(int size) {
            return new GroupMemberInfo[size];
        }
    };
}
